/**
@date: 2024/7/13
@note:
**/

package share_api

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"leading/global"
	"leading/models"
	"leading/models/res"
	"leading/service"
	"leading/utils"
	"strconv"
)

type ShareApi struct{}

// 创建分享
func (ShareApi) Create(c *gin.Context) {
	var query ShareCreateQuery
	if err := c.ShouldBind(&query); err != nil {
		res.FailWithoutData("参数有错", c)
		return
	}

	var user models.UserModel
	Res, _ := c.Get("user")
	user = Res.(models.UserModel)

	// 查看文件是否存在
	var file models.FileModel
	err := global.DB.Model(&models.FileModel{}).Where("id = ? and user_id = ?", query.FileId, user.ID).Find(&file).Error
	if err != nil {
		res.FailWithoutData(err.Error(), c)
		return
	}
	var shareModel models.ShareModel
	var shareNum string = uuid.New().String()
	shareModel.UserID = user.ID
	shareModel.FileID = query.FileId
	shareModel.IsCancel = 0
	shareModel.ShareUrl = "https://baidu.com/" + shareNum
	err = global.DB.Create(&shareModel).Error
	if err != nil {
		res.FailWithoutData("创建失败", c)
		return
	}
	res.OKWithoutMsg(gin.H{"url": shareModel.ShareUrl}, c)
}

// 我的分享列表
func (ShareApi) ShareList(c *gin.Context) {
	var shareModel []models.ShareModel
	var user models.UserModel
	Res, _ := c.Get("user")
	user = Res.(models.UserModel)

	err := global.DB.Where("user_id = ?", user.ID).Find(&shareModel).Error
	if err != nil {
		res.FailWithoutData(err.Error(), c)
		return
	}
	res.OKWithoutMsg(gin.H{"list": shareModel}, c)
}

/*
*
通过url 查看
查看分享
*/
func (ShareApi) SeeShare(c *gin.Context) {
	var shareModel models.ShareModel
	var query SeeShareQuery
	var user = utils.GetUser(c)
	if err := c.ShouldBind(&query); err != nil {
		res.FailWithoutData("参数有错", c)
		return
	}

	// 查看分享是否存在
	err := global.DB.Where("share_url = ? and user_id = ?", query.ShareUrl, user.ID).First(&shareModel).Error
	if err != nil {
		res.FailWithoutData("分享链接不存在", c)
		return
	}

	//
	var fileId uint = shareModel.FileID
	var file models.FileModel

	// 查看文件是否存在
	err = global.DB.Model(&models.FileModel{}).Where("id =? and user_id =?", fileId, user.ID).First(&file).Error
	if err != nil {
		res.OKWithoutMsg(gin.H{"list": []models.ShareModel{}}, c)
		return
	}

	// 判断是否为目录
	var isDir uint = file.IsDir
	var files []models.FileModel

	if isDir == 1 {
		err := global.DB.Where("file_id = ? and user_id = ?", file.ID, user.ID).Find(&files).Error
		if err != nil {
			res.FailWithoutData(err.Error(), c)
			return
		}
	} else {
		err := global.DB.Where("id = ? and user_id = ?", file.ID, user.ID).Find(&files).Error
		if err != nil {
			res.FailWithoutData(err.Error(), c)
			return
		}
	}
	res.OKWithoutMsg(gin.H{"list": files}, c)
}

// 保存到自己的网盘

func (ShareApi) SaveMyToFile(c *gin.Context) {
	var query SaveShareQuery
	if err := c.ShouldBind(&query); err != nil {
		res.FailWithoutData("参数有错", c)
		return
	}

	// 判断是否自己的分享
	var shareModel models.ShareModel
	err := global.DB.Where("share_url= ?", query.ShareUrl).First(&shareModel).Error
	if err != nil {
		res.FailWithoutData(err.Error(), c)
		return
	}

	var user = utils.GetUser(c)
	if shareModel.UserID == user.ID {
		res.FailWithoutData("本人分享，无需保存", c)
		return
	}

	// 判断目录是否存在
	if query.DirId > "0" {
		yes := service.IsDirExist(strconv.Itoa(int(shareModel.FileID)), shareModel.UserID)
		if !yes {
			res.FailWithoutData("目录不存在", c)
			return
		}
	}

	// 做递归，把文件保存到自己的网盘
	var canUseSize = user.TotalSize - user.UseSize
	if canUseSize == 0 {
		res.FailWithoutData("内存不足", c)
		return
	}
	var getAllFile func(string) bool
	getAllFile = func(fileId string) bool {
		// 获取目录下的所有文件
		var files []models.FileModel
		err := global.DB.Where("file_id= ? and user_id = ?", fileId, shareModel.UserID).Find(&files).Error

		if err != nil {
			res.FailWithoutData("文件保存失败", c)
			return false
		}

		for _, file := range files {
			if file.Size > canUseSize {
				res.FailWithoutData("内存不足", c)
				return false
			}
			tx := global.DB.Begin()
			var fileModel models.FileModel
			fileModel.FileId = query.DirId
			fileModel.UserID = user.ID
			fileModel.Name = file.Name
			fileModel.Size = file.Size
			fileModel.Ext = file.Ext
			fileModel.Md = file.Md
			fileModel.Url = file.Url
			fileModel.IsDir = file.IsDir
			err := tx.Create(&fileModel).Error
			if err != nil {
				res.FailWithoutData("保存失败", c)
				tx.Rollback()
				return false
			}
			tx.Commit()
			canUseSize -= file.Size
			// 需要更新剩余容量
			global.DB.Model(&models.UserModel{}).Where("id = ?", user.ID).Update("use_size", user.TotalSize-canUseSize)
			if file.IsDir == 1 {
				return getAllFile(file.FileId)
			}
		}
		return true
	}
	yes := getAllFile(strconv.Itoa(int(shareModel.FileID)))
	if yes {
		res.OKWithoutMsg(nil, c)
		return
	}
}
